Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Array → Add Array Items

Python Array

Add Array Items

Adding Array Items in Python: A Detailed Explanation

Python doesn't have a built-in array type in the same way as languages like C or Java. Instead, it uses lists, which are more flexible and dynamic. Lists can hold elements of different data types, whereas arrays (typically accessed through the `array` module or NumPy) usually require elements of the same type. We'll cover both approaches to illustrate adding items.

1. Adding Items to Lists>

Lists offer several methods for adding elements: `append()` Adds an element to the end of the list.
append() in Python my_list = [1, 2, 3] my_list.append(4) # Adds 4 to the end my_list.append("hello") # Adds a string print(my_list) # Output: [1, 2, 3, 4, \"hello\"]

Output

[1, 2, 3, 4, \"hello\"]

`insert()` Adds an element at a specific index. The existing elements at and after that index are shifted to the right.
insert() in Python my_list = [1, 2, 3] my_list.insert(1, 10) # Inserts 10 at index 1 print(my_list) # Output: [1, 10, 2, 3] my_list.insert(0, "start") # Insert at beginning print(my_list) #Output: ['start', 1, 10, 2, 3]

Output

[1, 10, 2, 3] ['start', 1, 10, 2, 3]

`extend()` Adds all elements from an iterable (like another list or tuple) to the end of the list.
extend() in Python my_list = [1, 2, 3] my_list.extend([4, 5, 6]) print(my_list) # Output: [1, 2, 3, 4, 5, 6] my_list.extend((7,8)) #Extend with a tuple print(my_list) # Output: [1, 2, 3, 4, 5, 6, 7, 8]

Output

[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 7, 8]

Concatenation List concatenation using `+` Creates a *new* list containing elements from both lists. The original lists remain unchanged.
Adding element using concatenation list1 = [1, 2, 3] list2 = [4, 5, 6] new_list = list1 + list2 print(new_list) # Output: [1, 2, 3, 4, 5, 6] print(list1) # Output: [1, 2, 3] - Original list unchanged

Output

[1, 2, 3, 4, 5, 6] [1, 2, 3]

2. Adding Items to Arrays (using the `array` module)

The `array` module allows creating arrays of a specific type. Adding items is less flexible than with lists because you're constrained by the data type.
Adding Items to Arrays (using the `array` module) import array # Create an array of integers my_array = array.array('i', [1, 2, 3]) # Append an integer my_array.append(4) print(my_array) # Output: array('i', [1, 2, 3, 4]) #Extend with another array. Must be same type code my_array.extend(array.array('i',[5,6])) print(my_array) # Output: array('i', [1, 2, 3, 4, 5, 6]) # Inserting is also possible, but less common: my_array.insert(1,10) #Insert 10 at index 1 print(my_array) # Output: array('i', [1, 10, 2, 3, 4, 5, 6]) #Attempting to add a float will result in an error #my_array.append(3.14) # This will raise a TypeError

Output

array('i', [1, 2, 3, 4]) array('i', [1, 2, 3, 4, 5, 6]) array('i', [1, 10, 2, 3, 4, 5, 6])

In summary, Python offers multiple ways to add items depending on your needs and the type of data structure you're using. Lists provide flexibility, while `array` and NumPy arrays offer performance benefits for numerical computations, albeit with some restrictions on data types and addition methods. Choose the approach that best suits your specific use case.

Tutorials